package com.example.qizhong.controller;

import com.example.qizhong.model.Student;
import com.example.qizhong.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class StudentController {

    @Autowired
    StudentService studentService;

    /**
     * 查询所有用户
     */

    @GetMapping("/student")
   //  @ApiOperation("查询所有用户")
    @CrossOrigin(origins = "http://localhost:8080")
    public List<Student> getAllStudent() {
        return studentService.list();
    }

    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable("id") Integer id) {
        Student student = studentService.getById(id);
        return student;
    }

    @PostMapping("/student")
    public String addUser(@RequestBody Student student) {
        if (studentService.save(student)) {
            return "插入用户成功！";
        } else return "插入用户失败";
    }

    /**
     * 修改用户信息
     *
     * @return
     */
    @PutMapping("/student")
    public String updateUser(@RequestBody Student student) {
        if (studentService.updateById(student)) {
            return "更新用户成功！";
        } else return "更新用户失败";
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/student")
    public String deleteUserById(Integer id) {
        if (studentService.removeById(id)) {
            return "删除用户成功！";
        } else return "删除用户失败！";
    }
}
